home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
net_des.arc
/
GETPASS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-12-05
|
1KB
|
52 lines
#include <stdio.h>
#include <signal.h>
#include <sgtty.h>
#ifdef MSDOS
#define TTY "con"
#else
#define TTY "/dev/tty" /* Change to "con" for MS-DOS */
#endif
/* Issue prompt and read reply with echo turned off */
char *
getpass(prompt)
char *prompt;
{
struct sgttyb ttyb,ttysav;
register char *cp;
int c;
FILE *tty;
static char pbuf[128];
int (*sig)();
if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
tty = stdin;
else
setbuf(tty, (char *)NULL);
sig = signal(SIGINT, SIG_IGN);
ioctl(fileno(tty), TIOCGETP, &ttyb);
ioctl(fileno(tty), TIOCGETP, &ttysav);
ttyb.sg_flags |= RAW;
ttyb.sg_flags &= ~ECHO;
ioctl(fileno(tty), TIOCSETP, &ttyb);
fprintf(stderr, "%s", prompt);
fflush(stderr);
cp = pbuf;
for (;;) {
c = getc(tty);
if(c == '\r' || c == '\n' || c == EOF)
break;
if (cp < &pbuf[127])
*cp++ = c;
}
*cp = '\0';
fprintf(stderr,"\r\n");
fflush(stderr);
ioctl(fileno(tty), TIOCSETP, &ttysav);
signal(SIGINT, sig);
if (tty != stdin)
fclose(tty);
return(pbuf);
}